#include "header.h"
//BOARD OBJECT SECTION
board::board() //Does nothing
{

}

void board::make() //Makes a board
{
  for (int n=0;n<10;n++)
  {
    //debugint("------------------------new room------------------------",n);
  buildRoom();
  }
  
  for (vector<room>::const_iterator firstRoom = rooms.begin(); firstRoom != rooms.end(); firstRoom++)
  {
    for (vector<room>::const_iterator secondRoom = rooms.begin(); secondRoom != rooms.end(); secondRoom++)
      {
        if(firstRoom!=secondRoom)
        {
          buildHallway(*firstRoom, *secondRoom); //Make a hallway!
        }
      }
  }
  
  movePlayer(rooms[0].x,rooms[0].y);
  playershape = char(001);
}// end make function

room board::buildRandomRoom() const
{
     return room( rand()%70+10
                , rand()%70+10
                , rand()%12 + 8
                , rand()%12 + 8
                //Select a foreground and background color
                , Color(static_cast<ColorName>(rand()%6 + 8),  //8 - 14 
                        static_cast<ColorName>(rand()%5 + 1)) //1 - 6  
                );
}

room board::buildNonCollidingRoom() const
{
     room newRoom;
     do  // this is bad, possible infinite loop, should use better logic eventually.
     {
          newRoom = buildRandomRoom();
     }
     while (checkCollideWithExistingRoom(newRoom));
     return newRoom;     
}

bool board::checkCollideWithExistingRoom(const room newRoom) const
{
     for (vector<room>::const_iterator currentRoom = rooms.begin(); currentRoom != rooms.end(); currentRoom++)
     {
         const room tempRoom = doRoomsCollide(newRoom, *currentRoom);
         if (tempRoom.x != -1)
            return true;
     }
     return false;
}

void board::buildRoom() //Builds a random room on the current board
{
  //char temp[1];
  const room newRoom = buildNonCollidingRoom();
  
  //Loop through data[x][y] and change the colors and make walkable
  for (int x=newRoom.x;x<newRoom.x+newRoom.w;x++)
  {
    for (int y=newRoom.y;y<newRoom.y+newRoom.h;y++)
    {
      data[x][y].color = newRoom.color;
      data[x][y].walkable = true;
      //itoa(rooms.size(),temp,10);
      //data[x][y].shape = temp[0];
    }// end of y for
  }// end of x for
  
  rooms.push_back(newRoom);
}

bool board::buildHallway(room one, room two) //Builds a hallway between the two rooms
{ //Returns true if it does, returns false if it can't
  //const int left = max(one.x,two.x);
  //const int right = min((one.x+one.w),(two.x+two.w));
  //const int top = max(one.y,two.y);
  //const int bottom = min((one.y+one.h),(two.y+two.h));
  
  //debug("Start hallway ============================================");
  room seg1;
  room seg2;
  if ( ( one.x < two.x+two.w && one.x+one.w > two.x) || (two.x < one.x+one.w && two.x > one.x+one.w) )
  {//This if checks to see if the rooms share any horizontal space
    //debug("Passed if statement, vertical hallway.");
    // Handles vertical hallways.
    if (one.y>two.y)
    {
      seg1 = buildVertHallway(one.x+(one.w/2), one.y, one.x+(one.w/2), two.y+two.h);
    }
    else
    {
      seg1 = buildVertHallway(one.x+(one.w/2), one.y+one.h, one.x+(one.w/2), two.y);
    }
  }//end vertical hallway if.
  
  if ( ( one.y < two.y+two.h && one.y+one.h > two.y) || (two.y < one.y+one.h && two.y > one.y+one.h) )
  {//This if checks to see if the rooms share any vertical space
    //debug("Passed if statement, horizontal hallway.");
    // Handles horizontal hallways.
    if (one.x>two.x)
    {
      seg1 = buildHorzHallway(one.x, one.y+(one.h/2), two.x+two.w, one.y+(one.h/2));
    }
    else
    {
      seg1 = buildHorzHallway(one.x+one.w, one.y+(one.h/2), two.x, one.y+(one.h/2));
    }
  }//end vertical hallway if.
  
  
}// End hallway builder

room board::buildVertHallway(int x1, int y1, int x2, int y2)
{
  room seg;
  int w = 1;
  int h = 1;
  int y=y2;
  if (y1>y2)
  {
    h = y1-y2;
  }
  else
  {
    h = y2-y1;
    y=y1;
  }
  seg = room(x1,y,w,h,Color(white, white));
  if (checkCollideWithExistingRoom(seg)==false)
  {
    drawVHallway(seg);
   }
  return seg;
}

room board::buildHorzHallway(int x1, int y1, int x2, int y2)
{
  room seg;
  int w = 1;
  int x = x2;
  int h=1;
  if (x1>x2)
  {
    w = x1-x2;
  }
  else
  {
    w = x2-x1;
    x = x1;
  }
   seg = room(x,y1,w,h,Color(white, white));
  if (checkCollideWithExistingRoom(seg)==false)
  {
    drawHHallway(seg);
  }
  return seg;
}

void board::drawVHallway(room seg)
{
  for (int n=seg.y;n<abs(seg.y+seg.h);n++)
  {
    data[seg.x][n].walkable = true;
    data[seg.x][n].color = Color(white, lightgray);
  }
}

void board::drawHHallway(room seg)
{
  for (int n=seg.x;n<abs(seg.x+seg.w);n++)
  {
    data[n][seg.y].walkable = true;
    data[seg.x][n].color = Color(white, lightgray);
  }
}

void board::draw() //This function initially draws a board, do not use this to update a board
{
  updateRect(room(0, 0, 80, 25));
}

void board::updateRect(room space)
{
  for (int x=space.x;x<space.x+space.w;x++)
  {
    for (int y=space.y;y<space.y+space.h;y++)
    {
      //if (screenx<=20 && screeny<=75 && screenx>=0 && screeny>=0)
      //{
        //Moves cursor to this x,y position
        //Sets the output color to the proper color as held in the terrain object
        cout << gotoxy(x,y) << data[x+screenx][y+screeny].color << getChar(x+screenx,y+screeny); //Outputs the character as held in the terrain object
      //} // end boundary if
    }
  }
  cout << gotoxy(0,0); //Moves back to the top left at the end
}

char board::getChar(int x, int y)
{
  if (x == playerx && y == playery)
    return playershape;
  
  if (data[x][y].explored==false)
  {
    cout << Color(white, black);
    return '?';
  }

  return data[x][y].shape;
}

//ROOM OBJECT SECTION

room::room(int nx, int ny, int nw, int nh, Color roomColor)
{
  x = nx;
  y = ny;
  w = nw;
  h = nh;
  color = roomColor;
}


room::room()
{
  x = -1;
  y = -1;
  w = -1;
  h = -1;
}
